package org.sjs.dreamfly.browser;

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import org.sjs.dreamfly.browser.jsbridge.JsBridge
import org.sjs.dreamfly.browser.jsbridge.JsInjector
import org.sjs.dreamfly.common.util.LogUtil

class PhemonWebView : WebView, JsInjector.InjectStrategy, JsInjector.InjectEventListener {
    constructor(context: Context) : this(context, null, 0, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        init()
    }

    private var pointFlag = 0x00
    private var methodFlag = 0x00

    /**
     * @param methods: InjectMethod
     * @param points: InjectPoint
     */
    fun inject(url: String, methods: Int = BrowserConst.InjectMethod.METHOD_REMOVE_ADS,
               points: Int = BrowserConst.InjectPoint.POINT_ON_PAGE_START) {
        LogUtil.d("url:$url, method:$methods, points:$points")
        methodFlag = methods
        pointFlag = points
        this.loadUrl(url)
    }

    fun useMacChromeUserAgent() {
        settings.userAgentString = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }

    fun useIPhoneUserAgent() {
        settings.userAgentString = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
    }

    @SuppressLint("WebViewApiAvailability")
    fun <T: WebViewClient> webViewClientAcceptVisitor(visitor: T) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            (webViewClient as PhemonWebViewClient).accept(visitor)
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun init() {
        webViewClient = PhemonWebViewClient(context, this).apply {
            addListener(this@PhemonWebView)
        }
        webChromeClient = PhemonWebChromeClient(context, this).apply {
            addListener(this@PhemonWebView)
        }
        setWebContentsDebuggingEnabled(true)
        settings.javaScriptEnabled = true
        settings.allowFileAccess = true
        settings.domStorageEnabled = true
        settings.databaseEnabled = true
        settings.loadsImagesAutomatically = true
        settings.loadWithOverviewMode = true;
        settings.useWideViewPort = true;
        settings.builtInZoomControls = true
        settings.displayZoomControls = true
        settings.setSupportZoom(true)
        settings.cacheMode = WebSettings.LOAD_DEFAULT
        this.setInitialScale(30)
        addJavascriptInterface(JsBridge, "JsBridge")
    }

    override fun onInjectEvent(url: String, point: Int, method: Int) {
        LogUtil.d("url:$url, point:$point, method:$method")
    }

    override fun getMethods(): Int {
        return methodFlag
    }

    override fun getPoints(): Int {
        return pointFlag
    }
}
